package cn.middle.remote.edge;

import cn.middle.remote.exception.RemoteException;
import cn.middle.remote.handler.client.ClientProcessingHandler;
import cn.middle.remote.message.EventReportSignal;
import cn.middle.remote.message.RegistryEdgeSignal;
import cn.middle.remote.util.Util;

public class NettyEdgeClient extends EdgeClient{

	@SuppressWarnings("rawtypes")
	@Override
	protected ClientProcessingHandler createProcessingHandler() {
		return new NettyEdgeProcessHandler();
	}

	@Override
	public void setListener(EdgeListener listener) {
		this.listener = listener;
	}

	@Override
	public <T> void event(int eventKey, String msgId, T msg) throws RemoteException {
		EventReportSignal signal = new EventReportSignal();
		if(Util.isNotNull(msgId)) {
			signal.setId(msgId);
		}
		signal.setEventId(eventKey);
		byte[] data = new byte[0];
		if(msg != null) {
			data = serialization.encode(msg);
		}
		signal.setData(data);
		send(signal);
	}

	@Override
	public <T> void registration(String msgId, T msg) throws RemoteException {
		RegistryEdgeSignal signal = new RegistryEdgeSignal();
		if(Util.isNotNull(msgId)) {
			signal.setId(msgId);
		}
		byte[] data = new byte[0];
		if(msg != null) {
			data = serialization.encode(msg);
		}
		signal.setData(data);
		send(signal);
	}

}
